/**
 * @projectName: api-interface
 * @package: com.wangzz.apiinterface.client
 * @className: hutoolClient
 * @author: wangzz
 * @description: TODO
 * @date: 2024/3/10 21:51
 * @version: 1.0
 */

package com.wangzz.apiclientsdk.client;

import cn.hutool.core.util.RandomUtil;
import com.wangzz.apiclientsdk.utils.SignUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wangzz
 * @Description: 模拟请求
 * @DateTime: 2024/3/10 21:51
 **/
public class ApiClient {

    protected final String accessKey;
    protected final String secretKey;

    protected static final String REQUEST_URL = "http://localhost:8090";

    public ApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }


    // 设置请求头
    protected Map<String, String> getHeaderMap(String body, String accessKey, String secretKey) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("accessKey", accessKey);
//        不能将密钥通过响应头传递
//        hashMap.put("secretKey",secretKey);
        hashMap.put("nonce", RandomUtil.randomNumbers(4));
        try {
            hashMap.put("body", URLEncoder.encode(body, StandardCharsets.UTF_8.name()));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        hashMap.put("timeStamp", String.valueOf(System.currentTimeMillis()));
        hashMap.put("sign", SignUtil.getSign(body, secretKey));
        return hashMap;
    }
}